﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chilkat;

namespace WebServiceClient
{
    public class AddAlgorithm : IAlgorithmAdd
    {
        private static readonly string domain = "localhost";
        private static readonly int port = 88;

        public void Add(string login, string password, string name, string desc, string code, List<bool> read, List<bool> write)
        {
            HttpRequest req = new HttpRequest();
            Http http = new Http();

            bool success;
            success = http.UnlockComponent("");

            http.CookieDir = "memory";
            http.SendCookies = true;
            http.SaveCookies = true;

            string html;
            html = http.QuickGetStr("http://" + domain + ":" + port + "/");

            req.UsePost();
            req.Path = "/";
            req.AddParam("l", login);
            req.AddParam("p", password);
            for (int i = 0; i < read.Count; i++)
            {
                req.AddParam("chb_read_" + i + 1, read[i] ? "1" : "0");
            }
            for (int i = 0; i < read.Count; i++)
            {
                req.AddParam("chb_write_" + i + 1, write[i] ? "1" : "0");
            }

            req.AddHeader("Referer", "http://" + domain + ":" + port + "/");

            bool ssl;
            ssl = false;
            HttpResponse resp;
            resp = http.SynchronousRequest(domain, port, ssl, req);

            req.Path = "/upload.htm";
            req.RemoveAllParams();
            req.AddParam("name", name);
            req.AddParam("desc", desc);
            req.AddParam("code", code);
            resp = http.SynchronousRequest(domain, port, ssl, req);
        }
    }
}
